Closed
handlers: guard latency metrics when timestamps are absent#1963
Conversation
When HandleResponseBody reaches end-of-stream it previously called all latency metric helpers unconditionally, including RecordNormalizedTimePerOutputToken, RecordRequestLatencies, RecordRequestTTFT, and RecordRequestTPOT. Those helpers reject zero/unordered timestamps and log errors, causing deterministic failures in tests that exercise response parsing or response size accumulation without initialising timing fields. Guard each metric call behind the timestamp checks it requires: - RecordNormalizedTimePerOutputToken and RecordRequestLatencies: only when RequestReceivedTimestamp and ResponseCompleteTimestamp are non-zero. - RecordRequestTTFT: only when RequestReceivedTimestamp and FirstTokenTimestamp are non-zero. - RecordRequestTPOT: only when all three timestamps are non-zero. - RecordResponseSizes is always recorded at end-of-stream. Add TestHandleResponseBodyZeroTimestamps to confirm that end-of-stream handling with zero timestamps records no latency metrics and still records the response size. Signed-off-by: GitHub Copilot <copilot@github.com>
Copilot
AI
changed the title
[WIP] Fix end-of-stream latency metrics for requests
handlers: guard latency metrics when timestamps are absent
Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HandleResponseBodyunconditionally called all latency metric helpers at end-of-stream. Those helpers enforce timestamp ordering and log errors when timestamps are zero — causing deterministic CI failures in tests that exercise parsing or response-size accumulation without a full request lifecycle (no timing fields set).Changes
pkg/epp/handlers/response.go: Wrap each latency metric call in the presence check it requires:RecordNormalizedTimePerOutputToken/RecordRequestLatencies: only whenRequestReceivedTimestampandResponseCompleteTimestampare non-zeroRecordRequestTTFT: only whenRequestReceivedTimestampandFirstTokenTimestampare non-zeroRecordRequestTPOT: only when all three timestamps are non-zeroRecordResponseSizes: always recorded (no timestamps required)pkg/epp/handlers/response_test.go: AddTestHandleResponseBodyZeroTimestamps— verifies that end-of-stream with zero timestamps emits no latency metrics and still records response size.Production behaviour is unchanged: in a real request all timestamps are set before
HandleResponseBodyis called, so the guards are never triggered.